package fr.umlv.escapeir.game;

import java.awt.Image;
import java.io.IOException;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.MathUtils;
import org.jbox2d.common.Vec2;

import fr.umlv.escapeir.GameWorld;
import fr.umlv.escapeir.gesture.Direction;
import fr.umlv.escapeir.painters.ImageLoader;

public class EnnemyShipFactory {
	
	private static final float ENNEMY_ANGLE = MathUtils.PI;
	
	private final Image imageJupiter;
	private final Image imageMoon;
	private final Image imageEarth;
	
	private final PolygonShape polygonJupiter;
	private final PolygonShape polygonMoon;
	private final PolygonShape polygonEarth;
	
	private final Vec2 topLeftEarth;
	private final Vec2 bottomRightEarth;
	private final Vec2 topLeftMoon;
	private final Vec2 bottomRightMoon;
	private final Vec2 topLeftJupiter;
	private final Vec2 bottomRightJupiter;
	private final Direction dir = Direction.DOWN;
	private final int value = 200;
	
	private final GameWorld world;
	
	public EnnemyShipFactory (GameWorld gameWorld) throws ShipCreationException {
		Image tmp = null;
		//JupiterShip
		try {
			tmp = ImageLoader.load("fr/umlv/escapeir/painters/images/ship1.png");
		} catch (IOException e1) {
			throw new ShipCreationException("Can't open image file for jupiterShip", e1);
		}
		imageJupiter = tmp;
		polygonJupiter = new PolygonShape();
		Vec2 verticesJupiter[] = new Vec2[6];
		verticesJupiter[0] = new Vec2(0, 23);
		verticesJupiter[1] = new Vec2(-16,0);
		verticesJupiter[2] = new Vec2(-16, -19);
		verticesJupiter[3] = new Vec2(0, -13);
		verticesJupiter[4] = new Vec2(16, -19);
		verticesJupiter[5] = new Vec2(16, 0);
		polygonJupiter.set(verticesJupiter, 6);
		polygonJupiter.m_centroid.set(0, 0);
		topLeftJupiter = new Vec2(-16,23);
		bottomRightJupiter = new Vec2(16,-23);
		
		//MoonShip
		try {
			tmp = ImageLoader.load("fr/umlv/escapeir/painters/images/ship2.png");
		} catch (IOException e1) {
			throw new ShipCreationException("Can't open image file for moonShip", e1);
		}
		imageMoon = tmp;
		polygonMoon = new PolygonShape();
		Vec2 verticesMoon[] = new Vec2[6];
		verticesMoon[0] = new Vec2(-10, 23);
		verticesMoon[1] = new Vec2(-19,0);
		verticesMoon[2] = new Vec2(-9, -13);
		verticesMoon[3] = new Vec2(9, -13);
		verticesMoon[4] = new Vec2(19, 0);
		verticesMoon[5] = new Vec2(10, 23);
		polygonMoon.set(verticesMoon, 6);
		polygonMoon.m_centroid.set(0, 0);
		topLeftMoon = new Vec2(-19,23);
		bottomRightMoon = new Vec2(19,-23);
		
		//EarthShip
		try {
			tmp = ImageLoader.load("fr/umlv/escapeir/painters/images/ship3.png");
		} catch (IOException e1) {
			throw new ShipCreationException("Can't open image file for earthShip", e1);
		}
		imageEarth = tmp;
		polygonEarth = new PolygonShape();
		Vec2 verticesEarth[] = new Vec2[6];
		verticesEarth[0] = new Vec2(-1, 21);
		verticesEarth[1] = new Vec2(-8,-2);
		verticesEarth[2] = new Vec2(-23, -14);
		verticesEarth[3] = new Vec2(23, -14);
		verticesEarth[4] = new Vec2(8, -2);
		verticesEarth[5] = new Vec2(1,21);
		polygonEarth.set(verticesEarth, verticesEarth.length);
		polygonEarth.m_centroid.set(0, 0);
		topLeftEarth = new Vec2(-23,21);
		bottomRightEarth = new Vec2(23,-21);
		
		world = gameWorld;
	}
	
	public WorldActor create (String ship, float x, float y) throws ShipCreationException {
		switch (ship) {
			case ("jupiter"):
				return createShipJupiter(x, y);
			case ("moon"):
				return createShipMoon(x, y);
			case ("earth"):
				return createShipEarth(x, y);
			default:
				throw new ShipCreationException("Ship doesn't exists");
		}
	}
	
	private WorldActor createShipJupiter (float x, float y) {
		PolygonShip ship = PolygonShip.createInWorld(world, x, y, ENNEMY_ANGLE, dir, value,
				polygonJupiter, imageJupiter,
				topLeftJupiter, bottomRightJupiter, GameWorld.WALL_INDEX);
		return ship;
	}
	private WorldActor createShipMoon (float x, float y) {
		PolygonShip ship = PolygonShip.createInWorld(world, x, y, ENNEMY_ANGLE, dir, value,
				polygonMoon, imageMoon,
				topLeftMoon, bottomRightMoon, GameWorld.WALL_INDEX);
		return ship;
	}
	private WorldActor createShipEarth (float x, float y) {
		PolygonShip ship = PolygonShip.createInWorld(world, x, y, ENNEMY_ANGLE, dir, value,
				polygonEarth, imageEarth,
				topLeftEarth, bottomRightEarth, GameWorld.WALL_INDEX);
		return ship;
	}
}
